const path = require('path')
const webpack = require('webpack')
const processVariable = require('./processVariable')
const fs = require('fs')
const preloadScript = fs.readFileSync(path.join(__dirname,'deps/preload.js'), 'utf8')
module.exports = {
  dev: processVariable.__DEV__,
  buildDir: processVariable.__DEV__ ? '.runtime':'.build',
  srcDir: 'src/',
  head: {
    title: '',
    meta: [
      {
        charset: 'utf-8'
      }, {
        name: 'viewport',
        content: 'width=device-width, initial-scale=1, maximum-scale=1,  minimum-scale=1, user-scalable=no'
      },
      {
        name:'wap-font-scale',
        content:'no'
      },
      {
        name: 'description',
        content: 'SMCB'
      }
    ],
    script: [{innerHTML: preloadScript}],
    __dangerouslyDisableSanitizers: ['script'],
    link: [
      {
        rel: 'icon',
        type: 'image/x-icon',
        href: '/favicon.ico'
      }
    ]
  },
  loading: false,
  css: [
    {
      src: '~assets/css/default.less',
      lang: 'less'
    }
  ],
  loaders: [
    {
      test: /\.(png|jpe?g|gif|svg|ico)$/,
      loader: 'url-loader',
      query: {
        limit: 100,
        name: '[name].[hash].[ext]'
      }
    }
  ],
  build: {
    //webpack编译分析
    analyze: false,
    extractCSS: true,
    extend(config, ctx) {
      if (ctx.isClient) {
        config.module.rules.push({
          enforce: 'pre',
          test: /\.(js|vue)$/,
          loader: 'eslint-loader',
          exclude: /(node_modules)/,
          options:{
            fix:true
          }
        })
        //全局css也通过postcss处理
        const cssLoader = config.module.rules.find((loader) => loader.test.toString() === '/\\.less$/')
        cssLoader.use.push({
          loader: 'postcss-loader'
        })
        cssLoader.exclude = /(node_modules)/
        // config.module.rules.push({
        //   test: /\.css$/,
        //   include: /(node_modules)/,
        //   use: processVariable.__DEV__
        //     ? ['vue-style-loader?sourceMap', 'css-loader?sourceMap']
        //     : ['vue-style-loader', 'css-loader']
        // })
      }
      config.resolve.alias['~types'] = config.resolve.alias['~'] + '/types'
      config.resolve.alias['~mocks'] = config.resolve.alias['~'] + '/mocks'
    },
    //webpack plugins
    plugins: [
      new webpack.DefinePlugin(processVariable)
    ]
  },
  plugins: [
  ],
  vendor: [
    'babel-polyfill',
    'axios'
  ]
}
